﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class DeviceInfoNote : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(DeviceInfoNote));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        public string sFileName = GlobleClass.UpFileType.DevPartfile.ToString();//"Devfile";

        #region 载入信息

        public void loadInfo()
        {
            BuyDevice buy = null;

            string sModelName = "";
            DataSet ds = null;
            try
            {
                buy = new BuyDevice();
                //inniDdlData(ddlPayWay,buy.getEnumDdl(2));
                if (m_Id == 0)
                {
                    if (!checkModelRightById(103, ref sModelName, (clsBaseLib)buy))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(104, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    else
                    {
                        if (!checkModelRightById(105, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    ds = buy.getDeviceInfoById(m_Id);
                    //hdselect.Value = buy.getDeviceSelectIds(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        txtDevID.Value = dt.Rows[0]["DeviceId"].ToString().Trim();
                        txtDevCode.Text = dt.Rows[0]["DeviceCode"].ToString().Trim();
                        dDevSource.SelectedValue = dt.Rows[0]["DevSource"].ToString().Trim();
                        txtDevName.Text = dt.Rows[0]["DeviceName"].ToString().Trim();
                        txtDevSpec.Text = dt.Rows[0]["Devicespec"].ToString().Trim();
                        txtType.Text = dt.Rows[0]["TypeName"].ToString().Trim();
                        txtDeptName.Text = dt.Rows[0]["DeptName"].ToString().Trim();
                        txtDeptId.Text = dt.Rows[0]["DeptId"].ToString().Trim();
                        btnDept.Visible = false;

                        txtEnterDate.Text = dt.Rows[0]["EnterDateTime"].ToString().Trim();
                        txtBuyDate.Text = dt.Rows[0]["BuyDateTime"].ToString().Trim();
                        txtPrice.Text = dt.Rows[0]["BuyPrice"].ToString().Trim();
                        txtBuyPrice.Text = dt.Rows[0]["BuyPrice"].ToString().Trim();
                        txtUseYear.Text = dt.Rows[0]["UseYear"].ToString().Trim();
                        dropState.SelectedValue = dt.Rows[0]["DeviceState"].ToString().Trim();
                        
                        txtCreater.Text = dt.Rows[0]["Creater"].ToString().Trim();
                        txtCreateTime.Text = dt.Rows[0]["CreateTime"].ToString().Trim();
                        txtProvider.Text = dt.Rows[0]["Provider"].ToString().Trim();
                        txtCreateCode.Text = dt.Rows[0]["ChuchangCode"].ToString().Trim();
                        txtCaiZhi.Text = dt.Rows[0]["Caizhi"].ToString().Trim();
                        txtWeight.Text = dt.Rows[0]["DevWeight"].ToString().Trim();
                        
                        txtComment.Text = dt.Rows[0]["Comment"].ToString().Trim();

                    }
                }
            }
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 获得设备

        //[Ajax.AjaxMethod]
        //public DataTable getSelectDeviceInfo(string sIds)
        //{
        //    BuyDevice buy = null;
        //    DataTable dt = null;
        //    try
        //    {
        //        buy = new BuyDevice();
        //        dt = buy.getResultDeviceInfo(sIds);
        //    }
        //    catch
        //    {
        //        dt = null;
        //    }
        //    finally
        //    {
        //        if (buy != null)
        //        {
        //            buy.Dispose();
        //        }
        //    }
        //    return dt;
        //} 

        #endregion

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int devid, int deptid, string buydate, int useyear, string dropstate, string dropsource, string devprice, string buyprice, string creater, string createdate, string provider, string createcode, string caizhi, string weight, string comment)
        {
            BuyDevice buy = new BuyDevice();
            bool bResult = false;
            buydate = buydate.Replace("'", "''");
            dropstate = dropstate.Replace("'", "''");
            creater = creater.Replace("'", "''");
            createdate = createdate.Replace("'", "''");
            provider = provider.Replace("'", "''");

            createcode = createcode.Replace("'", "''");
            caizhi = caizhi.Replace("'", "''");
            weight = weight.Replace("'", "''");
            comment = comment.Replace("'", "''");
            //spartcomment = spartcomment.Replace("'", "''");
            try
            {

                //for (int i = 0; i < sDevices.Length; i++)
                //{
                //    sDevices[i] = sDevices[i].Replace("'", "''");
                //}
                //devid, deptid, buydate, useyear, dropstate, creater, createdate, provider, createcode, caizhi, weight, comment
                bResult = buy.UpdateDeviceInfo(devid, deptid, buydate, useyear, dropstate, dropsource, devprice, buyprice, creater, createdate, provider, createcode, caizhi, weight, comment);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return bResult;
        }

        #endregion
    }
}